﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml.Linq;
using System.Data.Linq;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //DataTable dt = TextFileLoader.LoadTextFile("F:\\o.txt", "oo");
        //foreach (DataRow item in dt.Rows)
        //{
        //    ValidateRow(item);
        //}
        //Response.ContentType = "application/octet-stream";
        //    Response.AppendHeader("content-disposition", string.Format("attachment;filename={0}", "OutPutEmail.txt"));
        //    Response.Write(sb.ToString());
        //    Response.Flush();
        //    Response.End();
    }
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    System.Text.StringBuilder sbHeader = new System.Text.StringBuilder();
    static string[] RequiredColumns = { "学籍主号","姓名","性别","出生日期","民族","港澳台侨",
                                         "户籍类别","学生类别","入学方式","户籍所在省县",
                                         "户籍镇/街","家庭地址","监护人","监护人手机"
                                     };
    static string[] GAT = 
    {
        "无","归侨","华侨","侨眷","香港","澳门","台胞","外籍华人","华籍外人","非华裔外人"
    };
    string SchoolName = string.Empty;
    string GradeName = string.Empty;
    void  ValidateRow(DataRow dr)
    {
        foreach (string item in RequiredColumns)
        {
            if (dr[item] == null ||
                string.IsNullOrWhiteSpace(dr[item].ToString()) ||
                dr[item].ToString() == "0")
            {

                sb.AppendFormat("{0} {1} {2} “{3}” 字段数据空缺", new object[] { dr["学校"], dr["班级"], dr["姓名"], item });
                sb.Append("\r\n");
                switch (item)
                {
                    case "港澳台侨":
                        {
                            if (!GAT.Contains<string>(dr[item].ToString()))
                            {
                                sb.AppendFormat("{0} {1} {2} “{3}” 字段数据格式错误！", new object[] { dr["学校"], dr["班级"], dr["姓名"], item });
                                sb.Append("\r\n");
                            }
                            break;
                        }
                    case "入学方式":
                        {
                            if (dr["户籍所在省县"].ToString() == "杭州市余杭区" &&
                                dr["学校办学类别"].ToString() == "教育部门和集体办" &&
                                dr[item].ToString() != "施教区内")
                            {
                                sb.AppendFormat("{0} {1} {2} “{3}”字段数据错误，余杭区户籍学生入学方式应为施教区内！", new object[] { dr["学校"], dr["班级"], dr["姓名"], item });
                                sb.Append("\r\n");
                            }
                            break;
                        }
                    default:
                        break;
                }
            }
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (!FileUpload1.FileName.ToLower().EndsWith(".txt"))
        {
            return;
        }
        DataTable dt = TextFileLoader.LoadTextStream(
            this.FileUpload1.FileContent);
        uploadDiv.Visible = false;
        DataRow[] dtary = dt.AsEnumerable().OrderBy<DataRow, String>(o => o.Field<String>("学校"))
            .ThenByDescending<DataRow, String>(o => o.Field<String>("年级")).ToArray();
        int i=0;
        int j=0;
        foreach (DataRow item in dtary)
        {
            if (SchoolName != item["学校"].ToString())
            {
                if (!string.IsNullOrEmpty(SchoolName))
                {
                    sbHeader.AppendLine(string.Format("{0}共有{1}处需要完善修改！",SchoolName,i));
                }
                else
                {
                    GradeName = item["年级"].ToString();
                }
                SchoolName = item["学校"].ToString();
                i = 0;
            }
            else
            {
                if (GradeName!= item["年级"].ToString())
                {
                    if (!string.IsNullOrEmpty(GradeName) && !string.IsNullOrEmpty(SchoolName))
                    {
                        sbHeader.AppendLine(string.Format("{0} {2} 共有{1}处需要完善修改！", SchoolName, i, GradeName));
                    }
                    GradeName = item["年级"].ToString();
                    j = 0;
                }
            }
            ValidateRow(item);
            i++;
            j++;
        }
        if (!string.IsNullOrEmpty(SchoolName))
        {
            sbHeader.AppendLine(string.Format("{0}共有{1}需要完善修改！", SchoolName, i));
        }
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("content-disposition", string.Format("attachment;filename={0}", "OutPutEmail.txt"));
        Response.Write(sbHeader.Append(sb));
        Response.Flush();
        Response.End();
    }
    protected void FileUpload1_Init(object sender, EventArgs e)
    {

    }
}
